<html>
<head>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.min.js'></script>
<head>
	<style type="text/css">
		body {font-family: Arial;}
		.err {color:red; font-weight:bold;}
	</style>
</head>
<body>

<p>NOTE: test2 and test5 assume MongoDB running on default port, so those tests will fail if MongoDB is not available.</p>
<p>NOTE: test4 assumes Redis running on default port, so those tests will fail if Redis is not available.</p>

<p>ses.id (Redis)=<span id='sid'>N/A</span></p>
<p>ses.id (MongoDB)=<span id='sid2'>N/A</span></p>

<p>Test results: (<span id='completed'>0</span>/<span id='total'>0</span> POST/GET requests completed)</p>

<div id='res'>
</div>

<script type='text/javascript'>

var DOMAIN = 'localhost';
var PROTOCOL = 'http';
var g_testsTotal = 0;
var g_testsCompleted = 0;
var g_testResults = {};

function refreshResults() {
	$('#res').html( '' );
	var keys = [];
	for (name in g_testResults)
		keys.push(name);
	keys.sort();
	for (var i = 0 ; i < keys.length ; ++i) {
		var name = keys[i];
		$('#res').append( "<br/>"+name+': '+g_testResults[name] );
	}
}

function testGetJSON( name, port, path, fn ) {
	g_testsTotal++;
	refresh();
	var url = PROTOCOL+'://'+DOMAIN+':'+port+path;
	$.getJSON( url, function(data) {
		g_testResults[name] = (fn(data) ? 'ok' : '<span class="err">failed</span>: '+JSON.stringify(data));
		refreshResults();
	} );
}

function testPostJSON( name, port, path, data, fn ) {
	g_testsTotal++;
	refresh();
	var url = PROTOCOL+'://'+DOMAIN+':'+port+path;
	$.post( url, data, function(data) {
		$('#res').append( "<br/>"+name+" "+(fn(data) ? 'ok' : '<span class="err">failed</span>: '+JSON.stringify(data)) );
	} );
}

function refresh() {
	$('#completed').html( g_testsCompleted );
	$('#total').html( g_testsTotal );
}

function completed() {
	g_testsCompleted++;
	refresh();
}

// test core JServer
testGetJSON( "test1.1", 3001, "/hello", function(v){completed(); return v=="Hello anonymous";} );
testGetJSON( "test1.2", 3001, "/hello?name=Jani", function(v){completed(); return v=="Hello Jani";} );
testPostJSON( "test1.3", 3001, "/hello", "dummy=1", function(v){completed(); return v=="Hello anonymous";} );
testPostJSON( "test1.4", 3001, "/hello", "name=Jani", function(v){completed(); return v=="Hello Jani";} );

// test MongoDBC
testGetJSON( "test2.1", 3002, "/set?k=name&v=Jani", function(v){completed(); return v=="OK";} );
testGetJSON( "test2.2", 3002, "/get?k=name", function(v){completed(); return v=="Jani";} );
testGetJSON( "test2.3", 3002, "/findOneNotFound", function(v){completed(); return v.err === null && v.doc === null;} );

// test file serving
g_testResults['test3.1'] = "<img src='http://localhost:3003/images/test1.png' alt='not loaded' />";
g_testResults['test3.2'] = "<img src='http://localhost:3003/images/test2.png' alt='not loaded' />";
g_testResults['test3.3'] = "<img src='http://localhost:3003/images/test3.png' alt='not loaded' />";

// test sessions on redis
var sid = false;
function test_4_1() {testGetJSON( "test4.1", 3004, "/login?uname=jani", function(v){sid=v.sid; $('#sid').html(sid); test_4_2(); completed(); return v.data.uname=="jani";} );}
function test_4_2() {testGetJSON( "test4.2", 3004, "/ses?jsid="+sid, function(v){test_4_3(); completed(); return v.data.uname=="jani";} );}
function test_4_3() {testGetJSON( "test4.3", 3004, "/logout?jsid="+sid, function(v){test_4_4(); completed(); return v == "OK";} );}
function test_4_4() {testGetJSON( "test4.4", 3004, "/ses?jsid="+sid, function(v){completed(); return v.sid != sid;} );}
test_4_1();

// test sessions on mongodb
var sid2 = false;
function test_5_1() {testGetJSON( "test5.1", 3005, "/login?uname=jani", function(v){sid2=v.sid; $('#sid2').html(sid2); test_5_2(); completed(); return v.data.uname=="jani";} );}
function test_5_2() {testGetJSON( "test5.2", 3005, "/ses?jsid="+sid2, function(v){test_5_3(); completed(); return v.data.uname=="jani";} );}
function test_5_3() {testGetJSON( "test5.3", 3005, "/logout?jsid="+sid2, function(v){test_5_4(); completed(); return v == "OK";} );}
function test_5_4() {testGetJSON( "test5.4", 3005, "/ses?jsid="+sid2, function(v){completed(); return v.sid != sid2;} );}
test_5_1();

// test fileutil
testGetJSON( "test6.1", 3006, "/findtest6", function(v){completed(); return v=="test6-fileutil.js";} );

</script>

</body>
</html>
